package com.ibm.csorb.dao.impl;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.ibm.csorb.dao.CsCycleBtDao;
import com.ibm.csorb.entity.CsCycleBt;

@Component
@Transactional
public class CsCycleBtDaoImpl extends HibernateDaoSupport implements
		CsCycleBtDao {
	private static final Log log = LogFactory.getLog(CsCycleBtDaoImpl.class);
	// property constants
	public static final String CYCLE_ID = "cycleId";
	public static final String BUCKET_ID = "bucketId";
	public static final String STATUS = "status";
	public static final String SUCCESS = "success";
	public static final String FAILURE = "failure";

	protected void initDao() {
		// do nothing
	}

	public void save(CsCycleBt transientInstance) {
		log.debug("saving CsCycleBt instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(CsCycleBt persistentInstance) {
		log.debug("deleting CsCycleBt instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CsCycleBt findById(java.lang.Integer id) {
		log.debug("getting CsCycleBt instance with id: " + id);
		try {
			CsCycleBt instance = (CsCycleBt) getHibernateTemplate().get(
					"com.ibm.csorb.entity.CsCycleBt", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<CsCycleBt> findByExample(CsCycleBt instance) {
		log.debug("finding CsCycleBt instance by example");
		try {
			List<CsCycleBt> results = getHibernateTemplate().findByExample(
					instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<CsCycleBt> findByProperty(String propertyName, Object value) {
		log.debug("finding CsCycleBt instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from CsCycleBt as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<CsCycleBt> findByCycleId(Object cycleId) {
		return findByProperty(CYCLE_ID, cycleId);
	}

	public List<CsCycleBt> findByBucketId(Object bucketId) {
		return findByProperty(BUCKET_ID, bucketId);
	}

	public List<CsCycleBt> findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List<CsCycleBt> findBySuccess(Object success) {
		return findByProperty(SUCCESS, success);
	}

	public List<CsCycleBt> findByFailure(Object failure) {
		return findByProperty(FAILURE, failure);
	}

	@SuppressWarnings("unchecked")
	public List<CsCycleBt> findAll() {
		log.debug("finding all CsCycleBt instances");
		try {
			String queryString = "from CsCycleBt";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CsCycleBt merge(CsCycleBt detachedInstance) {
		log.debug("merging CsCycleBt instance");
		try {
			CsCycleBt result = (CsCycleBt) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CsCycleBt instance) {
		log.debug("attaching dirty CsCycleBt instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CsCycleBt instance) {
		log.debug("attaching clean CsCycleBt instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CsCycleBtDaoImpl getFromApplicationContext(
			ApplicationContext ctx) {
		return (CsCycleBtDaoImpl) ctx.getBean("CsCycleBtDAO");
	}
}